local FruitItem = class("FruitItem", function(fruitType,row,col,fruitTypeCount)
	math.newrandomseed();
	fruitType = fruitType or math.random(1,fruitTypeCount);
    local item = display.newSprite("#fruit" .. fruitType .. "_1.png");
    item.m_type = fruitType;
    item.m_row = row;
    item.m_col = col;
    item.m_isActive = false;
    return item;
end);

function FruitItem:ctor()
	
end

function FruitItem:setActive(active)
	self.m_isActive = active;

	local fileSuffix = active and "_2" or "_1";
	local fileName = string.format("%s%s%s.png","fruit",self.m_type,fileSuffix);
	local frame = display.newSpriteFrame(fileName);
	self:setSpriteFrame(frame);

	if active then
		self:stopAllActions();
		local scaleTo1 = cc.ScaleTo:create(0.1,1.1); 
		local scaleTo2 = cc.ScaleTo:create(0.05,1.0);
		local sx,sy = self:getPosition();
		local _,height = FruitItem.getSize();
		local jumpTo = cc.JumpTo:create(0.5,cc.p(sx,sy),height/20,2);
		self:runAction(cc.Sequence:create(scaleTo1,scaleTo2,jumpTo));
	end 
end 

function FruitItem:drop(diffY,time,speed)
	self:setTouchEnabled(false);
	diffY = tonumber(diffY) or 0;
	time = tonumber(time);
	speed = tonumber(speed) or 1;
	speed = speed == 0 and 1 or speed;

	local sx,sy = self:getPosition();
	local newPos = cc.p(sx,sy+diffY);
	self:setPosition(newPos);
	time = time or newPos.y/speed;
	self:stopAllActions();

	transition.moveTo(self,
		{x = sx, y = sy, time = time,
		onComplete = function()
			self:setTouchEnabled(true);
		end});	
end

function FruitItem.getSize()
	if not (g_fruitItemWidth or g_fruitItemHeight) then 
		--print("FruitItem.getSize..")
		local sprite = display.newSprite("#fruit1_1.png");
		local size = sprite:getContentSize();
		g_fruitItemWidth = size.width;
		g_fruitItemHeight = size.height;
	end 
	return g_fruitItemWidth,g_fruitItemHeight;
end 

return FruitItem;